package cn.one.ball.controller;

import cn.one.ball.entity.Banner;
import cn.one.ball.mapper.BannerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/banner/")
public class BannerController {
    @Autowired(required = false)
    BannerMapper mapper;
    @RequestMapping("select")
    public List<Banner> select(){
        return mapper.select();
    }
    @RequestMapping("delete")
    public void delete(int id){
        Banner banner = mapper.selectById(id);
        new File("d:/files"+banner.getUrl()).delete();
    }
    @RequestMapping("insert")
    public void insert(String url){
        System.out.println("url = " + url);
        mapper.insert(url);
    }
    @RequestMapping("selectById")
    public Banner selectById(int id){
        return mapper.selectById(id);
    }
    @RequestMapping("update")
    public void update(@RequestBody Banner banner){
        Banner oldBanner = mapper.selectById(banner.getId());
        //删除原来的图片文件
        new File("d:/files"+oldBanner.getUrl()).delete();
        mapper.update(banner);
    }
}
